#############################################################################
#Yuhua Wang#
#The Rise and Fall of Imperial China: The Social Origins of State Development#
#Princeton University Press, 2022#
############################################################################

############################################################################
#Chapter 6#
############################################################################

######################################
#Figure 6.1: Number of Local Single Whip Reforms (1531–1637)#
######################################

rm(list = ls())
library(foreign) 
library(ggplot2) 
require(ggplot2)
library(ggrepel) 
library(plyr)
require(scales)

#Change this to your local directory#

setwd("/Users/ywang/Dropbox/Book Project on State Building/Princeton UP/Replication Package/Chapter 6/data/")

singlewhipts <- read.dta("single whip event time series.dta")

attach(singlewhipts)

ggplot(data=singlewhipts, aes(x=year, y=count, color="black",linetype="solid")) + 
  geom_point(color="black", size = 0.5) + geom_line(color="black", size=0.6) +
  scale_x_continuous(name="Year", limits=c(1530,1640), breaks=c(1530,1540,1550,1560,1570,1580,1590,1600,1610,1620,1630,1640)) +
  scale_y_continuous(name="N of Single Whip Reforms",limits=c(0,25), breaks=c(0,5,10,15,20,25)) +
  theme_bw()+
  theme(legend.position="none")+
  theme(text = element_text(size=15, 
                            vjust=1, family="Times"))+ 
  geom_vline(xintercept = 1572,color="black",linetype="dashed")+ 
  geom_vline(xintercept = 1582,color="black",linetype="dashed")+ 
  annotate(geom="text", x = 1577, y=25, label = "Zhang Juzheng",size=4, fontface =2)+
  annotate(geom="text", x = 1577, y=24, label = "Government", size=4, fontface =2)

######################################
#Figure 6.2: Single Whip Implementation and Prefectural Representation in National Politics#
######################################

#Constructed in QGIS

######################################
#Figure 6.3: Social Network of Major Ming Officials and Their Kin (1573–1620)#
######################################

rm(list = ls())
library(igraph)
library(statnet)
library("network")

#Change this to your local directory#

setwd("/Users/ywang/Dropbox/Book Project on State Building/Princeton UP/Replication Package/Chapter 6/data/")

#Ming officials' adjacency matrix

mingmarriage<-read.csv("ming adjacency matrix.csv")

net.adj.mingmarriage <- as.matrix(mingmarriage) 

net_mingmarriage<-network(net.adj.mingmarriage, directed=FALSE, matrix.type="adjacency")

gden(net_mingmarriage)


#Ming officials and their kin

#read in egelist
rawedges<-read.csv("ming kinship edges.csv")

#turn edgelist into adjacency matrix
mingmatrix <-as.matrix(get.adjacency(graph.data.frame(rawedges)))

#write in a csv file
write.csv(mingmatrix,"adjmatrix.csv", row.names = FALSE)

#construct node attributes data in STATA

# detach igraph package
detach("package:igraph")

#Read in Adjacency Matrix

net.adj.ming <- as.matrix(mingmatrix) 

# load in vertex attributes

#orignal coordinates
nodeInfoMING <- read.csv(file="node attributes with id.csv",header=TRUE,stringsAsFactors=FALSE)

print(net.adj.ming) # peek at matrix 
print(nodeInfoMING)  # peek at attribute data


# create undirected network object from matrix

net_ming<-network(net.adj.ming, directed=FALSE, matrix.type="adjacency")

# it read in vertex names from matrix col names ...
network.vertex.names(net_ming)

# ATTACHING VERTEX ATTRIBUTES

# load in other attributes 
net_ming%v%"lon" <- nodeInfoMING$x_adj
net_ming%v%"lat" <- nodeInfoMING$y_adj


# Note: order of attributes in the data frame MUST match vertex ids
# otherwise the attribute will get assigned to the wrong vertex

# check that they got loaded
list.vertex.attributes(net_ming)

summary(net_ming)

gden(net_ming)

deg <- degree(net_ming,gmode="graph")

rescale<-function(nchar,low,high){
  min_d<-min(nchar)
  max_d<-max(nchar)
  rscl<-((high-low)*(nchar-min_d))/(max_d-min_d)+low
  rscl
}

#graph#

op<-par(mar=c(0,0,0,0))
plot(net_ming,vertex.cex=rescale(deg,0.5,3), edge.col="grey90",mode="fruchtermanreingold")
par(op)




